import { createRouter, createWebHashHistory } from 'vue-router'

import Layout from '@/layout/index.vue'

// 静态路由
const routes = [
  {
    path: '/',
    component: Layout,
    name: 'admin'
  },
  {
    path: '/login',
    component: () => import('@/view/Login/index.vue'),
    meta: {
      title: '登录页'
    }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/view/404.vue'),
    meta: {
      title: '404'
    }
  },
]

// 动态路由
const asyncRoutes = [
  {
    path: '/',
    name: '/',
    component: () => import('@/view/index.vue'),
    meta: {
      title: '后台首页'
    }
  },
  {
    path: '/goods/list',
    name: '/goods/list',
    component: () => import('@/view/Goods/index.vue'),
    meta: {
      title: '商品管理'
    }
  },
  {
    path: '/category/list',
    name: '/category/list',
    component: () => import('@/view/Category/index.vue'),
    meta: {
      title: '分类列表'
    }
  },
  {
    path: '/user/list',
    name: '/user/list',
    component: () => import('@/view/User/index.vue'),
    meta: {
      title: '用户管理'
    }
  },
  {
    path: '/comment/list',
    name: '/comment/list',
    component: () => import('@/view/Comment/index.vue'),
    meta: {
      title: '评论管理'
    }
  },
  {
    path: '/order/list',
    name: '/order/list',
    component: () => import('@/view/Order/index.vue'),
    meta: {
      title: '订单管理'
    }
  },
  {
    path: '/image/list',
    name: '/image/list',
    component: () => import('@/view/Image/index.vue'),
    meta: {
      title: '图库管理'
    }
  },
  {
    path: '/notice/list',
    name: '/notice/list',
    component: () => import('@/view/Notice/index.vue'),
    meta: {
      title: '公告管理'
    }
  },
  {
    path: '/setting/base',
    name: '/setting/base',
    component: () => import('@/view/Setting/index.vue'),
    meta: {
      title: '系统设置'
    }
  },
  {
    path: '/coupon/list',
    name: '/coupon/list',
    component: () => import('@/view/Coupon/index.vue'),
    meta: {
      title: '优惠券管理'
    }
  },
  {
    path: '/manager/list',
    name: '/manager/list',
    component: () => import('@/view/Manager/index.vue'),
    meta: {
      title: '管理员管理'
    }
  },
  {
    path: '/access/list',
    name: '/access/list',
    component: () => import('@/view/Access/index.vue'),
    meta: {
      title: '权限管理'
    }
  },
  {
    path: '/role/list',
    name: '/role/list',
    component: () => import('@/view/Role/index.vue'),
    meta: {
      title: '角色管理'
    }
  },
  {
    path: '/skus/list',
    name: '/skus/list',
    component: () => import('@/view/Skus/index.vue'),
    meta: {
      title: '规格管理'
    }
  },
  {
    path: '/level/list',
    name: '/level/list',
    component: () => import('@/view/Level/index.vue'),
    meta: {
      title: '会员等级'
    }
  }
]

export const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

export const addRoutes = menus => {
  let hasNewRoutes = false
  const findRoutesByMenus = arr => {
    arr.forEach(e => {
      let item = asyncRoutes.find(o => o.path == e.frontpath)
      if (item && !router.hasRoute(item.path)) {
        router.addRoute('admin', item)
        hasNewRoutes = true
      }
      if (e.child && e.child.length > 0) {
        findRoutesByMenus(e.child)
      }
    })
  }
  findRoutesByMenus(menus)
  return hasNewRoutes
}
